<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

/**
 * Main controller
 */
class Controller_Document extends Controller_Template {
	protected $_template = 'other/master';

	/**
	 * List of documents
	 */
	public function indexAction($cat_id = -1, $page_id = 1) {
		if ($cat_id > -1) {
			Session::set('document_cat_id', intval($cat_id));
		} else if (Session::is('document_cat_id')) {
			$cat_id = intval(Session::get('document_cat_id'));
		} else {
			$cat_id = 0;
		}

		$options = array(
			'page_id' => $page_id,
			'page_size' => 10,
			'id' => Input::get('doc_id'),
			'name' => Input::get('doc_name'),
			'alias' => Input::get('doc_alias'),
			'schema_id' => intval(Input::get('doc_schema')),
			'status_id' => intval(Input::get('doc_status'))
		);

		$filter_url = '';
		$filter_on = false;
		
		if (!empty($options['id'])) {
			$filter_url .= empty($filter_url) ? '?' : '&';
			$filter_url .= 'doc_id=' . $options['id'];
		}

		if (!empty($options['name'])) {
			$filter_url .= empty($filter_url) ? '?' : '&';
			$filter_url .= 'doc_name=' . $options['name'];
		}

		if (!empty($options['alias'])) {
			$filter_url .= empty($filter_url) ? '?' : '&';
			$filter_url .= 'doc_alias=' . $options['alias'];
		}

		if (!empty($options['status_id'])) {
			$filter_url .= empty($filter_url) ? '?' : '&';
			$filter_url .= 'doc_status=' . $options['status_id'];
		}

		if (!empty($options['schema_id'])) {
			$filter_url .= empty($filter_url) ? '?' : '&';
			$filter_url .= 'doc_schema=' . $options['schema_id'];
		}

		if (!empty($filter_url)) {
			$filter_on = true;
		}
		
		$this->template->content = View::factory('document/index')
			->set('cat_id', $cat_id)
			->set('page_id', $page_id)
			->set('count', Document::getCount($cat_id, $options))
			->set('options', $options)
			->set('filter_on', $filter_on)
			->set('filter_url', $filter_url)
			->set('cat_list', Category::getFullList(1))
			->set('doc_list', Document::getList($cat_id, $options));
	}

	/**
	 * Insert document
	 */
	public function insertAction($error = -1) {
		if (Input::isPostBack()) {
			$doc_alias = Router::fixUrl(Input::post('doc_alias'));
			if (!empty($doc_alias)) {
				if (Document::getIdByAlias($doc_alias) > 0) {
					$doc_alias = null;
				}
			} else {
				$doc_alias = null;
			}

			$data = array();
			Query::data($data, 'name', Input::post('doc_name'), Query::DATA_STRING);
			Query::data($data, 'alias', $doc_alias, Query::DATA_STRING);
			Query::data($data, 'status_id', Input::post('doc_status_id'), Query::DATA_INTEGER);
			Query::data($data, 'schema_id', Input::post('doc_schema_id'), Query::DATA_INTEGER);
			Query::data($data, 'insert_user_id', User::getId(), Query::DATA_INTEGER);
			Query::data($data, 'insert_time', time(), Query::DATA_INTEGER);

			$new_doc_id = Document::insert($data);
			
			if ($new_doc_id) {
				Document::addToCategory($new_doc_id, Input::post('doc_cat_id'));
				Tools::redirect(BASE_URL . 'document/update/' . $new_doc_id . '/-2');
			} else {
				Tools::redirect(CURRENT_URL . '1');
			}
		} else {
			$cat_id = 0;
			if (Session::is('document_cat_id')) {
				$cat_id = intval(Session::get('document_cat_id'));
			}
			
			$view = View::factory('document/insert')
				->set('error', $error)
				->set('schema_list', Schema::getList(1))
				->set('cat_id', $cat_id);
			
			$this->template->content = $view->render();
		}
	}

	/**
	 * Update document
	 */
	public function updateAction($id = 0, $error = -1) {
		if (Input::isPostBack()) {
			$doc_alias = Router::fixUrl(Input::post('doc_alias'));
			if (!empty($doc_alias)) {
				if (Document::getIdByAlias($doc_alias, $id) > 0) {
					$doc_alias = null;
				}
			} else {
				$doc_alias = null;
			}

			$data = array();
			Query::data($data, 'name', Input::post('doc_name'), Query::DATA_STRING);
			Query::data($data, 'alias', $doc_alias, Query::DATA_STRING);
			Query::data($data, 'status_id', Input::post('doc_status_id'), Query::DATA_INTEGER);
			Query::data($data, 'update_user_id', User::getId(), Query::DATA_INTEGER);
			Query::data($data, 'update_time', time(), Query::DATA_INTEGER);

			if (Document::update($id, $data)) {
				Tools::redirect(CURRENT_URL . $id . '/0');
			} else {
				Tools::redirect(CURRENT_URL . $id . '/1');
			}
		} else {
			$doc_info = Document::getInfo($id);
			$schema_info = Schema::getInfo($doc_info['schema_id'], 'file');
			$this->template->content = View::factory('document/update')
				->set('error', $error)
				->set('info', $doc_info)
				->set('schema_file', $schema_info['file'])
				->render();
		}
	}

	public function deleteAction($doc_id, $cat_id = 0, $error = -1) {
		if (Input::isPostBack()) {
			$delete_from = intval(Input::post('delete_from'));
			$delete_code = 0;
			if (1 == $delete_from) {
				$data = array();
				Query::data($data, 'status_id', 3, Query::DATA_INTEGER);
				Query::data($data, 'delete_user_id', User::getId(), Query::DATA_INTEGER);
				Query::data($data, 'delete_time', time(), Query::DATA_INTEGER);
				if (!Document::delete($doc_id, $data)) {
					$delete_code = 1;
				}
			} else if (2 == $delete_from) {
				$cat_list = Input::post('delete_from_cats');
				foreach ($cat_list as $cat) {
					Document::removeFromCategory($doc_id, $cat);
				}
			}
			
			Tools::redirect(CURRENT_URL . $doc_id . '/' . $cat_id . '/' . $delete_code);
		} else {
			$cat_list = array();
			$cats = Document::inCategories($doc_id);
			foreach ($cats as $cat) {
				$cat_list[] = array(
					'id' => $cat,
					'path' => Category::getFullPath($cat)
				);
			}

			$this->template->content = View::factory('document/delete')
				->set('error', $error)
				->set('cat_id', $cat_id)
				->set('info', Document::getInfo($doc_id , 'id,name'))
				->set('cat_list', $cat_list)
				->render();
		}
	}

	public function sequenceAction($doc_id = 0, $cat_id = 0, $direction = 0) {
		$error = -1;
		if ($direction > 0) {
			$result = false;

			if (1 == $direction) {
				$result = Document::moveUp($doc_id, $cat_id);
			} else if (2 == $direction) {
				$result = Document::moveDown($doc_id, $cat_id);
			}
			
			if (!$result) {
				$error = 1;
			}
		}
		
		$view = View::factory('document/sequence')
			->set('error', $error)
			->set('info', Document::getInfo($doc_id))
			->set('cat_id', $cat_id)
			->set('doc_id', $doc_id);

		if ($cat_id > 0) {
			$view->set('doc_list', Document::getList($cat_id, array('status_id' => array(1,2))));
		} else {
			$cat_list = array();
			$cats = Document::inCategories($doc_id);
			foreach ($cats as $cat) {
				$cat_list[] = array(
					'id' => $cat,
					'path' => Category::getFullPath($cat)
				);
			}
			$view->set('cat_list', $cat_list);
		}

		$this->template->content = $view->render();
	}

	public function categoriesAction($doc_id, $cat_id = 0, $action = 0) {
		$error = -1;
		if (($cat_id > 0) && ($action > 0)) {
			$result = false;

			if (1 == $action) {
				$result = Document::addToCategory($doc_id, $cat_id);
			} else if (2 == $action) {
				$result = Document::removeFromCategory($doc_id, $cat_id);
			}

			if (!$result) {
				$error = 1;
			}
		}

		$cat_list = array();
		$cats = Document::inCategories($doc_id);
		foreach ($cats as $cat) {
			$cat_list[] = array(
				'id' => $cat,
				'path' => Category::getFullPath($cat)
			);
		}
		
		$this->template->content = View::factory('document/categories')
			->set('error', $error)
			->set('info', Document::getInfo($doc_id))
			->set('doc_id', $doc_id)
			->set('full_cat_list', Category::getFullList(1))
			->set('cat_list', $cat_list)
			->render();
	}
}